const expressJoi = require('@escook/express-joi')
const db = require('../db/index')
const utils = require('../utils/index')
const fileEvent = require('../utils/file')

module.exports = {
    async reqGetproTableList(req, res) {
        let obj = req.body
        let sql = 'select * from userlist where 1=1'
        sql = utils.setLike(sql, 'username', obj.username)
        let { total } = await utils.getSum({ sql, name: 'userlist', res, req })
        sql = utils.pageSize(sql, obj.pageNum, obj.pageSize)
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            res.send({ data: { list: results, pageNum: obj.pageNum, pageSize: obj.pageSize, total: obj.username ? results.length : total } })
        })
    },
    reqGetproTableGender(req, res) {
        let sql = 'select * from gender'
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            res.send({ code: 200, data: results, msg: '成功' })
        })
    },
    reqGetproTableStatus(req, res) {
        let sql = 'select * from status'
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            res.send({ code: 200, data: results, msg: '成功' })
        })
    },
    async reqUploadImg(req, res) {
        
        let result = await fileEvent(req, res);
        res.set('Content-Type', 'application/json; charset=utf-8'); // 设置内容类型为 JSON
        res.send({ data: result[0] })
    },
    reqAddproTable(req, res) {
        let sql = 'insert into userlist set ?'
        const { username, gender, age, idCard, email, address, avatar, id } = req.body
        let obj = { username, gender, age, idCard, email, address, avatar }
        db.query(sql,obj, (err, resutls) => {
            if (err) return res.cc(err)
            res.send({ code: 200, msg: '添加成功' })
        })
    },
    reqEditproTable(req, res) {
        let sql = `update userlist set ? where id=?`
        const { username, gender, age, idCard, email, address, avatar } = req.body
        let obj = { username, gender, age, idCard, email, address, avatar }
        let { id } = req.body
        db.query(sql, [obj, id], (err, results) => {
            if (err) return res.cc(err)
            res.send({ code: 200, msg: '修改成功' })
        })
    },
    reqDeleteproTable(req, res) {
        let sql = `DELETE FROM userlist WHERE id IN (?)`;
        const ids = req.body.id;
        db.query(sql, ids, (err, results) => {
            if (err) return res.cc(err)
            res.send({ code: 200, msg: '删除成功' })
        })
    },
    reqChangeproTable(req, res) {
        let sql = `update userlist set ? where id=?`
        let { id, status } = req.body
        db.query(sql, [{status}, id], (err, resutls) => {
            if (err) return res.cc(err)
            res.send({ code: 200, msg: '修改成功' })
        })
    }


}